package com.tianhua.javs.client.bean;

import com.google.common.collect.Lists;
import com.tianhua.javs.client.model.FieldModel;
import com.tianhua.javs.client.model.MethodModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Bean;

import java.util.*;

/**
 * Description:
 * date: 2022/2/11
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
public class JavsTranslateContextBean {

    /**
     * javs中的业务模型
     * 业务类对应的可调动的函数或者对象资源
     * bo,dto,entity,do,cmd,event,msgbody
     */
    private Map<String,Map<String,JavsModelBean>> javsModelBeanMap = new HashMap<>();

    /**
     * 领域服务元模型
     * 在翻译的时候进行参考
     */
    private DomainModelBean domainModelBean;

    /**
     * className.methodName映射
     * 如userMapper.insert --> MethodModel(insert)
     *
     */
    private Map<String, List<MethodModel>> classMethodMap = new HashMap<>();

    /**
     * model.fieldName映射
     * 如userBO.userName ---> FieldModel(userName)
     */
    private Map<String, FieldModel> modelFieldMap = new HashMap<>();

    /**
     * 当前在解析的脚本方法
     */
    private JavsScriptMethodBean currentScriptMethodBean;


    public JavsScriptMethodBean getCurrentScriptMethodBean() {
        return currentScriptMethodBean;
    }

    public void setCurrentScriptMethodBean(JavsScriptMethodBean currentScriptMethodBean) {
        this.currentScriptMethodBean = currentScriptMethodBean;
    }

    public Map<String,JavsModelBean> getJavsModelBeanMap(String modelTag){
        return this.javsModelBeanMap.get(modelTag);
    }


    public Map<String, List<MethodModel>> getClassMethodMap() {
        return classMethodMap;
    }

    public Map<String, FieldModel> getModelFieldMap() {
        return modelFieldMap;
    }

    public DomainModelBean getDomainModelBean() {
        return domainModelBean;
    }

    public void setDomainModelBean(DomainModelBean domainModelBean) {
        this.domainModelBean = domainModelBean;
    }


    public void addJavsModelBean(String modelTag, String classBOName,JavsModelBean javsBOBean){
        Map<String,JavsModelBean> modelBeanMap = javsModelBeanMap.get(modelTag);
        if(modelBeanMap == null){
            modelBeanMap = new HashMap<>();
        }
        modelBeanMap.put(classBOName, javsBOBean);
        javsModelBeanMap.put(modelTag, modelBeanMap);
    }

    /**
     * 增加classmethod映射模型
     * @param classMethodKey
     * @param methodModel
     */
    public void addClassMethod(String classMethodKey,MethodModel methodModel){
        List<MethodModel> methodModelList = this.classMethodMap.get(classMethodKey);
        if(CollectionUtils.isEmpty(methodModelList)){
            methodModelList = Lists.newArrayList(methodModel);
        }
        methodModelList.add(methodModel);

        this.classMethodMap.put(classMethodKey.toLowerCase(), methodModelList);
    }

    /**
     * 增加模型属性映射模型
     * @param modelFieldKey
     * @param fieldModel
     */
    public void addModelField(String modelFieldKey,FieldModel fieldModel){
        this.modelFieldMap.put(modelFieldKey, fieldModel);
    }


    /**
     * 根据类名 方法名  方法参数数量找到一个目标方法
     * @param className
     * @param methodName
     * @param paramCount
     * @return
     */
    public MethodModel getByClassMethod(String className,String methodName,int paramCount){
        String key = className+"."+methodName;
        List<MethodModel> methodModelList = this.getClassMethodMap().get(key.toLowerCase());
        if(CollectionUtils.isEmpty(methodModelList)){
            return null;
        }

        Optional<MethodModel> methodModelOptional = methodModelList.stream().filter(methodModel -> methodModel.getParamModelList().size() == paramCount).findFirst();
        if(methodModelOptional.isPresent()){
            return methodModelOptional.get();
        }
        return null;
    }

}


